using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Vin.Tool.AspNetCore.Singleton;

namespace Vin.Tool.AspNetCore;

[ApiController]
[Route("api/[controller]/[action]")]
public class VinController : ControllerBase
{
    protected ILogger Logger =>
        VinApp.GetRequiredService<ILoggerFactory>().CreateLogger(this.GetType());

    public VinController()
    {
        VinApp.Inject(this);
    }

    protected virtual IActionResult Success<T>(T? obj = default, int total = 0)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, 200, "请求成功", obj!, total));
    }

    protected virtual IActionResult Success<T>(int code, string msg = "请求成功", T? obj = default, int total = 0)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, code, msg, obj!, total));
    }

    protected virtual IActionResult SuccessOther<T>(int code, string msg = "请求成功", T? obj = default, int total = 0,
        params object[] others)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, code, msg, obj!, total, others));
    }

    protected virtual IActionResult Success<T>(T? obj = default, string msg = "请求成功", int total = 0, params
        object[] others)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, 200, msg, obj!, total));
    }

    protected virtual IActionResult Success()
    {
        return Success<object>(200);
    }

    protected virtual IActionResult Error<T>(int code, string msg = "请求失败", T? obj = default, int total = 0)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, code, msg, obj!, total));
    }

    protected virtual IActionResult ErrorOther<T>(int code, string msg = "请求失败", T? obj = default, int total = 0, params
        object[] others)
    {
        return Ok(Activator.CreateInstance(VinApp.ApiResultType, code, msg, obj!, total, others));
    }

    protected virtual IActionResult Error(string msg)
    {
        return Error<object>(500, msg);
    }

    protected virtual IActionResult Excel(byte[] data, string name, string contentType = "application/vnd.ms-excel")
    {
        return File(data, contentType, name.Contains('.') ? name : name + ".xlsx");
    }
}